package pers.vic.blog.module.front.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import pers.vic.blog.constant.GlobalFieldParams;
import pers.vic.blog.module.api.model.BlogComment;
import pers.vic.blog.module.api.model.GlobalConfig;
import pers.vic.blog.module.api.service.GlobalConfigService;
import pers.vic.blog.module.front.model.TimelineModel;
import pers.vic.blog.module.front.service.BlogHomeService;
import pers.vic.blog.module.front.service.OtherService;
import pers.vic.boot.base.lookup.Lookup;
import pers.vic.boot.base.model.BaseResponse;
import pers.vic.boot.base.model.PageInfo;

/**
 * @author Vic.xu
 * @description: 一些其他的页面的访问
 * @date: 2020/6/11 0011 9:14
 */
@Controller
public class OtherController extends BaseFrontController{

    @Autowired
    private OtherService otherService;

    @Autowired
    private BlogHomeService blogHomeService;

    @Autowired
    private GlobalConfigService globalConfigService;

    /**
     * 关于我
     */
    @GetMapping(value = "about")
    public String  about(Model model ){
        GlobalConfig about = globalConfigService.getByCode(GlobalFieldParams.GlobalConfigCodeEnum.about);
        model.addAttribute("about", about.getContent());
        return "about";
    }


    /**
     * 日志归档 get
     */
    @GetMapping("/timeline")
    public String timeline(@ModelAttribute("lookup") Lookup lookup, Model model){
        PageInfo<TimelineModel> pageInfo = otherService.timelinePage(lookup);
        model.addAttribute("pager", pageInfo);
        return "timeline";
    }

    /**
     * 日志归档 post
     * @param lookup
     * @param attributes
     * @return
     */
    @PostMapping("timeline")
    public String searchTimeline(Lookup lookup, RedirectAttributes attributes){
        attributes.addFlashAttribute("lookup", lookup);
        return "redirect:timeline";
    }

    /* ****************************************************************************************** */

    /**
     * 前往留言板页面
     */
    @GetMapping("/comment")
    public String comment(@ModelAttribute("lookup") BlogComment lookup, Model model){
        PageInfo<BlogComment> pager = otherService.comments(lookup);
        model.addAttribute("pager", pager);
        return "comment";
    }

    /**
     * 留言板页面分页
     */
    @PostMapping("/comment")
    @ResponseBody
    public String comment(BlogComment lookup, RedirectAttributes attributes){
        attributes.addFlashAttribute("lookup", lookup);
        return "redirect:comment";
    }

    /**
     * 异步加载留言板数据
     * @param lookup
     * @return
     */
    @PostMapping("/commentMore")
    @ResponseBody
    public BaseResponse commentMore(BlogComment lookup){
        PageInfo<BlogComment> pager = otherService.comments(lookup);
        return BaseResponse.success(pager);
    }


    @PostMapping("saveComment")
    @ResponseBody
    public BaseResponse saveComment(BlogComment entity){
        int  num = otherService.saveComment(entity);
        return BaseResponse.success();

    }



}
